﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using PccCommonForC;
using System.Text.RegularExpressions;
using System.IO;

public partial class Manager_Product_AddFilm : System.Web.UI.Page
{
    string Fname;
    protected string sOldName = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminUserID"] == null)
        {
            Response.Write("<script language=javascript>alert('Time is out.');window.open('../Default.aspx','_top');</script>");
            return;
        }
        Ajax.Utility.RegisterTypeForAjax(typeof(Manager_Product_AddProduct));
        if (!IsPostBack)
        {
            BindCatalogueByTopicID();
            BindData();
        }

    }


    private void BindRate(DropDownList ddl)
    {

        ListItem item = new ListItem();
        item.Text = "VNĐ";
        item.Value = "VNĐ";
        item.Selected = true;
        ddl.Items.Add(item);
        item = new ListItem();
        item.Text = "USD";
        item.Value = "USD";
        ddl.Items.Add(item);

    }

    [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.ReadWrite)]
    public string AjaxCheckPD(string value, string PDID)
    {
        string flag = "no";
        clsWebShop cls = new clsWebShop();
        DataTable dt = new DataTable();
        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("PDName", value.Trim());
        msg.CreateFirstNode("ExcPDID", PDID);
        dt = cls.Get_ProductByPDNm(msg.GetXmlStr.ToString()).Tables[0];
        if (dt.Rows.Count > 0)
            flag = "yes";
        return flag;
    }
    [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.ReadWrite)]
    public DataSet AjaxGetCatByTopicID(string value)
    {
        DataSet ds = null;
        clsWebShop cls = new clsWebShop();
        ds = cls.Get_CatalogueByTopicIDNoneProc(value);
        return ds;
    }







    private void BindData()
    {
        PccMsg msg = new PccMsg();

        if (Request.QueryString["PDID"] != "" && Request.QueryString["PDID"] != null)
        {
            clsWebShop cls = new clsWebShop();
            msg.CreateFirstNode("PDID", Request.QueryString["PDID"]);
            msg.CreateFirstNode("CatID", "0");
            msg.CreateFirstNode("TopicID", "0");
            msg.CreateFirstNode("StID", "0");
            DataTable dt = cls.Get_FilmAdminByCondition(msg.GetXmlStr.ToString()).Tables[0];
            if (dt.Rows.Count > 0)
            {

                txtFilmNo.Text = dt.Rows[0]["FilmNo"].ToString();
                txtTenSP.Text = dt.Rows[0]["FilmEng"].ToString();
                txtFilmNameVN.Text = dt.Rows[0]["FilmVnd"].ToString();
                txtYear.Text = dt.Rows[0]["FilmYear"].ToString();
                txtHero.Text = dt.Rows[0]["Hero"].ToString();
                txtDirector.Text = dt.Rows[0]["Director"].ToString();
                txtSound.Text = dt.Rows[0]["Sound"].ToString();
                txtSubtitles.Text = dt.Rows[0]["Subtitles"].ToString();

                txtCapacity.Text = dt.Rows[0]["Capacity"].ToString();
                txtFormat.Text = dt.Rows[0]["Format"].ToString();
                FCKeditor1.Value = dt.Rows[0]["Content"].ToString();

                clsCommon clsCom = new clsCommon();         
      
                if (dt.Rows[0]["IsHot"].ToString() == "Y")
                    chkHot.Checked = true;


                string sURL = dt.Rows[0]["imgURL"].ToString();
                if (sURL != "" && sURL != null)
                {
                    imgShowImage.Visible = true;
                    imgShowImage.Style.Add("cursor", "pointer");
                    imgURL.Src = "../../" + sURL;
                    hImg.Value = sURL;
                }

                //////////////                 

                FCKeditor1.Value = dt.Rows[0]["Content"].ToString();

                txtCapacity.Text = dt.Rows[0]["Capacity"].ToString().ToUpper().Replace("GB", "").Trim();

            }
        }
    }




    private void BindCatalogueByTopicID()
    {
        clsWebShop cls = new clsWebShop();

        ListItem item = new ListItem();
        DataTable dt = null;
        dt = cls.Get_CatalogueFilmByTopicIDNoneProc("0").Tables[0];
        ddlTheLoai.DataSource = dt;
        ddlTheLoai.DataTextField = "Catname";
        ddlTheLoai.DataValueField = "CatID";
        ddlTheLoai.DataBind();
        item.Text = "-- Chọn thể loại--";
        item.Value = "0";
        ddlTheLoai.Items.Insert(0, item);
        try
        {
            if (Request.QueryString["CatID"] != "" && Request.QueryString["CatID"] != null)
                ddlTheLoai.Items.FindByValue(Request.QueryString["CatID"]).Selected = true;
        }
        catch (Exception ex) { }
    }

    private string QueryStringStr(string str)
    {
        return str + "=" + Request.QueryString[str];

    }
    private void ReturnPage()
    {
        Response.Redirect("Film.aspx?KindNo=" + Request.QueryString["KindNo"] + "&CodeID=" + Request.QueryString["CodeID"] + "&" + QueryStringStr("sCatID") + "&" + QueryStringStr("sTopicID") + "&" + QueryStringStr("sPdNm") + "&PageIndex=" + Request.QueryString["PageIndex"]);
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        ReturnPage();
    }

    protected void btnUpdate_Click(object sender, EventArgs e)
    {

        if (FileU_HinhMinhHoa.HasFile)
        {
            Regex objRegex = new Regex(@"(.*?)\.jpg|(.*?)\.png|(.*?)\.gif|(.*?)\.bmp");
            if (!objRegex.IsMatch(FileU_HinhMinhHoa.FileName.ToLower()))
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Chọn file hình ảnh')</script>");
                return;
            }
            else
            {
                FileInfo file = new FileInfo(Server.MapPath("~") + "/Images/Film/" + hImg.Value);
                if (file.Exists)
                    file.Delete();


                string FPath, strdate;
                strdate = System.DateTime.Today.ToString("yyyyMMddhhmmss");
                Fname = "Images/Products/" + strdate + FileU_HinhMinhHoa.FileName;
                FPath = Server.MapPath("~/") + Fname;
                FileU_HinhMinhHoa.SaveAs(FPath);

            }
        }
        else // Giu lai file cu
            Fname = hImg.Value;




        string PDID = "0";
        if (Request.QueryString["PDID"] != "" && Request.QueryString["PDID"] != null)
        {
            PDID = Request.QueryString["PDID"];
        }
        string sBaoHanh = "0";
        if (txtSound.Text != "")
            sBaoHanh = txtSound.Text;

        //Hot
        string hot = "N";
        if (chkHot.Checked == true)
            hot = "Y";
        string Status = "N";




        PccMsg msg = new PccMsg();
        msg.CreateFirstNode("FilmID", PDID);
        msg.CreateFirstNode("FilmNo", txtFilmNo.Text);
        msg.CreateFirstNode("CatID", ddlTheLoai.SelectedValue);
        msg.CreateFirstNode("FilmEng", txtTenSP.Text);
        msg.CreateFirstNode("FilmVnd", txtFilmNameVN.Text);
        msg.CreateFirstNode("FilmYear", txtYear.Text);
        msg.CreateFirstNode("ImgURL", Fname);
        msg.CreateFirstNode("Hero", txtHero.Text);
        msg.CreateFirstNode("Director", txtDirector.Text);
        msg.CreateFirstNode("National", txtNational.Text);
        msg.CreateFirstNode("Sound", txtSound.Text);
        msg.CreateFirstNode("Subtitles", txtSubtitles.Text);
        msg.CreateFirstNode("Capacity", txtCapacity.Text);
        msg.CreateFirstNode("Format", txtFormat.Text);
        msg.CreateFirstNode("Content", FCKeditor1.Value.ToString());
        msg.CreateFirstNode("IsHot", hot);
        msg.CreateFirstNode("Status", Status);
        msg.CreateFirstNode("UserID", Session["AdminUserID"].ToString());

        clsWebShop cls = new clsWebShop();
        string xml=cls.Ins_Upd_Film(msg.GetXmlStr.ToString());
        msg = new PccMsg(xml);
        if (msg.Query("strOutput") == "-5")
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Mã phim này đã tồn tại!!!');</script>");
            return;

        }
        

        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Success!!!');</script>");

        //       ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script>alert('Success!!!');window.location.href='AddProduct.aspx'</script>");

        ClientScript.RegisterClientScriptBlock(this.GetType(), "", "<script> alert('Dữ liệu đã cập nhật thành công');</script>");
        ReturnPage();

    }

    private string CheckIsNull(string value)
    {
        string sReturn = "0";
        if (value != "" && value != null) sReturn = value;
        return sReturn;
    }
    private string CheckIsNullNoZero(string value)
    {
        string sReturn = "-1";
        if (value != "" && value != null) sReturn = value;
        return sReturn;
    }



}
